﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Composite.Data;
using System.Text.RegularExpressions;
using LCA.Common.Calculation;

public partial class LeftNavCategory : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            LoadFlowTree();
            LoadSystemTree();
        }
    }

    private void LoadSystemTree()
    {
        
        using (DataConnection conn = new DataConnection())
        {
            var roots = from fc in conn.Get<LCA.Data.LCASystemCategory>()
                        where fc.IsDeleted == false && fc.Parent == AppSettings.CategoryId.SystemRoot
                        orderby fc.Ordinal
                        select fc;

            treeSystems.Text += "<ul>";

            foreach (LCA.Data.LCASystemCategory cat in roots)
            {
                // Recursively retrieve its child
                treeSystems.Text += string.Format("<li data-jstree='' id='{0}'>{1}{2}{3}</li>", cat.Id, GetSystemCatChild(conn, cat.Id), cat.Name, GetSystemsBySystemCategory(conn, cat.Id));

            }

            treeSystems.Text += "</ul>";

        }

    }

    private string GetSystemsBySystemCategory(DataConnection conn, Guid guid)
    {
        string result = "";
        var systems = from fcf in conn.Get<LCA.Data.LCASystemCategorySystem>()
                    join f in conn.Get<LCA.Data.LCASystem>() on fcf.LCASystem equals f.Id
                    where f.IsDeleted == false && fcf.LCASystemCategory == guid
                    orderby fcf.Ordinal
                    select f;

        if (systems.Count() > 0)
        {
            result += "<ul>";
            foreach (LCA.Data.LCASystem system in systems)
            {
                string selected = "";
                result += string.Format("<li data-jstree='{0}' id='{1}'>{2}</li>", "{ " + selected + " \"url\": \"" + GetFriendlyUrl(system.Name, "system", system.LCASystemId) + "\" , \"icon\": \"/Frontend/Images/default_16.png\"}", system.Id, system.Name);

            }
            result += "</ul>";
        }

        return result;
    }

    private string GetSystemCatChild(DataConnection conn, Guid guid)
    {
        string result = "";

        var childs = from fc in conn.Get<LCA.Data.LCASystemCategory>()
                     where fc.IsDeleted == false && fc.Parent == guid
                     orderby fc.Ordinal
                     select fc;

        if (childs.Count() > 0)
        {
            result += "<ul>";

            foreach (LCA.Data.LCASystemCategory cat in childs)
            {
                result += string.Format("<li data-jstree='' id='{0}'>{1}{2}{3}</li>", cat.Id, GetSystemCatChild(conn, cat.Id), cat.Name, GetSystemsBySystemCategory(conn, cat.Id));

            }

            result += "</ul>";
        }

        return result;
    }

    private void LoadFlowTree()
    {   
        using (DataConnection conn = new DataConnection())
        {
            var roots = from fc in conn.Get<LCA.Data.FlowCategory>()
                        where fc.IsDeleted == false && fc.Parent == AppSettings.CategoryId.FlowRoot
                        orderby fc.Ordinal
                        select fc;

            treeFlows.Text += "<ul>";

            foreach (LCA.Data.FlowCategory cat in roots)
            {
                // Recursively retrieve its child
                treeFlows.Text += string.Format("<li data-jstree='' id='{0}'>{1}{2}{3}</li>", cat.Id, GetChild(conn, cat.Id), cat.Name, GetFlowsByFlowCategory(conn, cat.Id));
               //treeFlows.Text += GetFlowsByFlowCategory(conn, cat.Id);
            }

            treeFlows.Text += "</ul>";

        }
    }

    private string GetChild(DataConnection conn, Guid guid)
    {
        string result = "";

        var childs = from fc in conn.Get<LCA.Data.FlowCategory>()
                     where fc.IsDeleted == false && fc.Parent == guid
                     orderby fc.Ordinal
                     select fc;

        if (childs.Count() > 0)
        {
            result += "<ul>";

            foreach (LCA.Data.FlowCategory cat in childs)
            {
                result += string.Format("<li data-jstree='' id='{0}'>{1}{2}{3}</li>", cat.Id, GetChild(conn, cat.Id), cat.Name, GetFlowsByFlowCategory(conn, cat.Id));
                
            }

            result += "</ul>";
        }

        return result;
    }

    private string GetFlowsByFlowCategory(DataConnection conn, Guid guid)
    {
        string result = "";
        var flows = from fcf in conn.Get<LCA.Data.FlowCategoryFlow>()
                    join f in conn.Get<LCA.Data.Flow>() on fcf.Flow equals f.Id
                    where f.IsDeleted == false && fcf.FlowCategory == guid
                    orderby fcf.Ordinal
                    select f;

        if (flows.Count() > 0)
        {
            result += "<ul>";
            foreach (LCA.Data.Flow flow in flows)
            {
                string selected = "";
                result += string.Format("<li data-jstree='{0}' id='{1}'>{2}</li>", "{ " + selected + " \"url\": \"" + GetFriendlyUrl(flow.Name, "flow", flow.FlowId ) + "\" , \"icon\": \"/Frontend/Images/default_16.png\"}", flow.Id, flow.Name);
            
            }
            result += "</ul>";
        }

        return result;
    }

    private object GetFriendlyUrl(string name, string type,  int flowId)
    {
        // string title = HttpUtility.UrlEncode(GetUrlFromTitle(name).ToLower());
        return string.Format("/viewer/{0}/{1}", type, flowId.ToString());
    }

    public string GetUrlFromTitle(string title)
    {
        return Regex.Replace(title, @"[^\w\d ]+", string.Empty).Replace(" ", "-");

    }
}